<?php
/**
 * Created by PhpStorm.
 * User: *!N.J
 * Date: 2021/10/9
 * Time: 22:27
 * QQ:1467572213
 */

namespace app\admin\model;

use think\Model;
use think\Db;
use think\facade\Config;
use think\facade\Session;

class Count extends Model
{
    public static function index($start_time,$end_time,$pid_name)
    {
        $s_time = strtotime($start_time);
        $e_time = strtotime($end_time) + 1 * 24 * 3600;

        if ($pid_name){
            $agent_id = Db::name('fd_user')->where('username',$pid_name)->value('id');
        }

        $reg_num = [
            ['pid', '=', $agent_id],
            ['status', '=', 1],
            ['create_time', '>', $s_time],
            ['create_time', '<', $e_time],
        ];
        $info['reg_num'] = Db::name('fd_user')->where($reg_num)->count();
        
        $c_day_shou = [
            ['type', '=', 1],
            ['status', '=', 1],
            ['create_time', '>', $s_time],
            ['create_time', '<', $e_time],
        ];
        $info['c_day_shou'] = Db::name('fd_recharge')->where($c_day_shou)->group('uid')->sum('money');

        $c_recharge = [
            ['pid', '=', $agent_id],
            ['type', '=', 1],
            ['status', '=', 1],
            ['create_time', '>', $s_time],
            ['create_time', '<', $e_time],
        ];
        $info['c_recharge'] = Db::name('fd_recharge')->where($c_recharge)->sum('money');
        $info['c_recharge_ren'] = Db::name('fd_recharge')->where($c_recharge)->count();

        $service_cz = [
            ['pid', '=', $agent_id],
            ['type', '=', 1],
            ['status', '=', 1],
            ['create_time', '>', $s_time],
            ['create_time', '<', $e_time],
        ];
        $info['service_cz'] = Db::name('fd_recharge')->where($service_cz)->sum('service_charge');

        $m_recharge = [
            ['pid', '=', $agent_id],
            ['type', '=', 2],
            ['status', '=', 1],
            ['create_time', '>', $s_time],
            ['create_time', '<', $e_time],
        ];
        $info['m_recharge'] = Db::name('fd_recharge')->where($m_recharge)->sum('money');

        $m_withdrawal = [
            ['pid', '=', $agent_id],
            ['type', '=', 2],
            ['status', '=', 1],
            ['create_time', '>', $s_time],
            ['create_time', '<', $e_time],
        ];
        $info['m_withdrawal'] = Db::name('fd_withdrawal')->where($m_withdrawal)->sum('actual_money');
        $info['m_withdrawal_ren'] = Db::name('fd_withdrawal')->where($m_withdrawal)->count();

        $m_withdrawal_cost = [
            ['pid', '=', $agent_id],
            ['type', '=', 2],
            ['status', '=', 1],
            ['create_time', '>', $s_time],
            ['create_time', '<', $e_time],
        ];
        $info['m_withdrawal_cost'] = Db::name('fd_withdrawal')->where($m_withdrawal_cost)->sum('service_money');

        $grab_num = [
            ['aid', '=', $agent_id],
            ['create_time', '>', $s_time],
            ['create_time', '<', $e_time],
        ];
        $info['grab_num'] = Db::name('fd_order')->where($grab_num)->count();


        $z_recharge = Db::name('fd_recharge')->where($c_recharge)->count();
        $s_recharge = Db::name('fd_recharge')->where($m_recharge)->count();

        $info['r_number'] = $z_recharge + $s_recharge;

        $info['profit'] = $info['c_recharge'] + $info['m_recharge'] - $info['m_withdrawal'];
        return $info;
    }

}